package com.tanhua.server.service;

import com.itheima.dubbo.api.UserApi;
import com.itheima.dubbo.api.UserInfoApi;
import com.tanhua.autoconfig.template.ApiFaceTemplate;
import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.autoconfig.template.SmsTemplate;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserThreadLocal;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.Duration;

/**
 * @Author Pan Rundong
 * @Date 2023-02-25 21:04
 * @Desc 用户详细信息Service
 */
@Service
public class UserInfoService {
    @Autowired
    private SmsTemplate smsTemplate;
    @Autowired
    private OssTemplate ossTemplate;
    @Autowired
    private ApiFaceTemplate apiFaceTemplate;
    @Autowired
    private RedisTemplate redisTemplate;
    @DubboReference(version = "1.0.0")
    private UserInfoApi userInfoApi;
    @DubboReference(version = "1.0.0")
    private UserApi userApi;

    /**
     * 保存用户信息
     * @Author: Pan Rundong
     * @param userInfo
     * @return void
     */
    public void save(UserInfo userInfo) {
        //获取用户Id
        Long userId = UserThreadLocal.getUserId();
        //存入userInfo
        userInfo.setId(userId);
        //保存
        userInfoApi.save(userInfo);
    }

    /**
     * 检查图片是否为人脸
     * @Author: Pan Rundong
     * @param multipartFile
     * @return void
     */
    public void saveImage(MultipartFile multipartFile) throws IOException {
        //1.上传阿里云，获取url
        String imageUrl = ossTemplate.upload(multipartFile.getOriginalFilename(), multipartFile.getInputStream());

        //2.调用apiFace方法验证
        boolean detect = apiFaceTemplate.detect(imageUrl);

        //3.若不为人脸，抛出图像错误异常
        if (!detect) {
            throw new BusinessException(ErrorResult.faceError());
        } else {
            //4.若为人脸，调用userInfoApi，修改图片信息
            Long userId = UserThreadLocal.getUserId();
            UserInfo userInfo = new UserInfo();
            userInfo.setId(userId);
            userInfo.setAvatar(imageUrl);
            userInfoApi.update(userInfo);
        }
    }

    /**
     * 根据Id查询
     * @Author: Pan Rundong
     * @param id
     * @return com.tanhua.model.domain.UserInfo
     */
    public UserInfoVo getById(Long id) {
        //1.调用dubbo
        UserInfo userInfo = userInfoApi.findById(id);

        //2.设置vo
        UserInfoVo userInfoVo = new UserInfoVo();

        //3.复制
        BeanUtils.copyProperties(userInfo, userInfoVo);

        //4.将年龄字段存入vo
        StringBuilder sb = new StringBuilder(userInfo.getAge());
        String age = sb.toString();
        if(age != null) {
            userInfoVo.setAge(age);
        }

        //5. 返回
        return userInfoVo;
    }

    /**
     * 更新用户信息
     * @Author: Pan Rundong
     * @param userInfo
     * @return void
     */
    public void update(UserInfo userInfo) {
        //1.调用api
        userInfoApi.update(userInfo);
    }

    /**
     * 修改手机号1-发送验证码
     *
     * @param phone
     * @return void
     * @Author Pan Rundong
     */
    public void sendCode(String phone) {
        //1.生成验证码
        String code = "123456";
        //2.发送验证码
//        smsTemplate.sendSms(phone, code);
        //3.存Redis
        redisTemplate.opsForValue().set("CHECK_CODE_" + phone, code, Duration.ofMinutes(5));
    }

    /**
     * 修改手机号2-验证验证码
     *
     * @param verificationCode
     * @return void
     * @Author Pan Rundong
     */
    public void verification(String verificationCode, String phone) {
        //1.从redis获得验证码
        String code = (String) redisTemplate.opsForValue().get("CHECK_CODE_" + phone);

        //2.判断是否与用户输入的验证码相同
        if (!verificationCode.equals(code)) {
            //2.1不同，抛出异常
            throw new BusinessException(ErrorResult.loginError());
        }
        //2.2相同，删除redis缓存数据
        redisTemplate.delete("CHECK_CODE_" + phone);
    }

    /**
     * 修改手机号3-保存手机号
     *
     * @param phone
     * @return void
     * @Author Pan Rundong
     */
    public void updatePhone(String phone) {
        //1.获取用户Id
        Long userId = UserThreadLocal.getUserId();
        //2.封装user对象
        User user = new User();
        user.setId(userId);
        user.setMobile(phone);

        //3.远程调用更新信息
        userApi.update(user);
    }
}
